package com.graduation.check.service.impl;

import com.graduation.check.dto.*;
import com.graduation.check.mapper.TeacherSubmissionMapper;
import com.graduation.check.service.TeacherSubmissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TeacherSubmissionServiceImpl implements TeacherSubmissionService {
    
    @Autowired
    private TeacherSubmissionMapper teacherSubmissionMapper;
    
    @Override
    public SubmissionListResponse getSubmissionList(SubmissionListRequest request) {
        log.info("开始获取提交列表 - page: {}, size: {}", request.getPage(), request.getSize());
        try {
            // 计算offset
            int offset = (request.getPage() - 1) * request.getSize();
            
            // 创建新的请求对象用于查询，保持原始请求不变
            SubmissionListRequest queryRequest = new SubmissionListRequest();
            queryRequest.setPage(offset);
            queryRequest.setSize(request.getSize());
            queryRequest.setStudentId(request.getStudentId());
            queryRequest.setClassName(request.getClassName());
            queryRequest.setMajor(request.getMajor());
            queryRequest.setTaskType(request.getTaskType());
            queryRequest.setStatus(request.getStatus());
            queryRequest.setStartDate(request.getStartDate());
            queryRequest.setEndDate(request.getEndDate());
            queryRequest.setMinSimilarity(request.getMinSimilarity());
            queryRequest.setMaxSimilarity(request.getMaxSimilarity());
        
            // 查询提交列表
            List<SubmissionListItem> records = teacherSubmissionMapper.selectSubmissionList(queryRequest);
        
            // 查询总数
            Long total = teacherSubmissionMapper.countSubmissions(queryRequest);
        
            // 查询统计信息
            Map<String, Object> stats = teacherSubmissionMapper.getSubmissionStatistics(queryRequest);
        
            // 组装响应
            SubmissionListResponse response = new SubmissionListResponse();
            response.setTotal(total);
            response.setPage(request.getPage());
            response.setSize(request.getSize());
            response.setRecords(records);
        
            // 设置统计信息
            SubmissionListResponse.StatisticsInfo statistics = new SubmissionListResponse.StatisticsInfo();
            statistics.setTotalSubmissions(((Number) stats.getOrDefault("totalSubmissions", 0)).intValue());
            statistics.setCompletedSubmissions(((Number) stats.getOrDefault("completedSubmissions", 0)).intValue());
            statistics.setProcessingSubmissions(((Number) stats.getOrDefault("processingSubmissions", 0)).intValue());
            statistics.setFailedSubmissions(((Number) stats.getOrDefault("failedSubmissions", 0)).intValue());
        
            Object avgSim = stats.get("avgSimilarity");
            statistics.setAvgSimilarity(avgSim != null ? ((Number) avgSim).doubleValue() : 0.0);
        
            statistics.setHighRiskCount(((Number) stats.getOrDefault("highRiskCount", 0)).intValue());
            statistics.setMediumRiskCount(((Number) stats.getOrDefault("mediumRiskCount", 0)).intValue());
            statistics.setLowRiskCount(((Number) stats.getOrDefault("lowRiskCount", 0)).intValue());
        
            response.setStatistics(statistics);
        
            log.info("提交列表获取成功 - total: {}", total);
            return response;
        } catch (Exception e) {
            log.error("获取提交列表失败", e);
            throw new RuntimeException("获取提交列表失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public Map<String, Object> getSubmissionDetail(Long submissionId) {
        return teacherSubmissionMapper.getSubmissionDetail(submissionId);
    }
    
    @Override
    public String batchDownloadReports(List<Long> submissionIds, String downloadType) {
        // TODO: 实现批量下载功能
        return "reports_batch_" + System.currentTimeMillis() + ".zip";
    }
}
